package cycling.taxi.com.ui.activity;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import cycling.taxi.com.R;
import cycling.taxi.com.entity.CommonQuestion;
import cycling.taxi.com.entity.Tips;
import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.ui.fragment.TipsFrgt;
import cycling.taxi.com.utils.Urls;

public class AutofixActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView back;
    private ListView lv_qustion;
    private List<CommonQuestion> data;
    private  MyQustionAdapter adapter;
    private RequestQueue queue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autofix);
        ExitApplication.getInstance().addActivity(this);
        back=(ImageView)findViewById(R.id.back);
        back.setOnClickListener(this);
        lv_qustion=(ListView)findViewById(R.id.lv_qustion);
        queue= Volley.newRequestQueue(this);
        adapter = new MyQustionAdapter();
        queue= Volley.newRequestQueue(this);
        volleyGet();

    }
    public void volleyGet(){
       // String path="http://192.168.1.100:8080/CyclingBgTV1/appRepairByself/repairByself.dop";
        String path = Urls.getReparByself();
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                data = new Gson().fromJson(s,new TypeToken<List<CommonQuestion>>(){}.getType());
                lv_qustion.setAdapter(adapter);
            }
        },null);
        queue.add(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
        }
    }
    public class MyQustionAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView==null) {
                convertView = View.inflate(AutofixActivity.this, R.layout.qustion_item, null);
            }
                CommonQuestion cq = data.get(position);
                TextView tv_qustion = (TextView) convertView.findViewById(R.id.tv_qustion);
                tv_qustion.setText(cq.getQuestionDescription()+"---"+cq.getAnswer());


            return convertView;
        }
    }
}
